package com.flyduck.controller;


import com.flyduck.common.lang.Result;
import com.flyduck.service.SysMenuService;
import com.flyduck.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author flyduck
 * @since 2024-05-27
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 获取当前用户的菜单和权限信息
     * @param principal
     * @return
     */
    @GetMapping("/nav")
    public Result<UserPermRespVO> nav(Principal principal){
        UserPermRespVO userPermRespVO = sysMenuService.getNavListByUserId(principal.getName());
        return Result.success(userPermRespVO);
    }

    @PreAuthorize("hasAuthority('sys:menu:list')")
    @GetMapping("/info/{id}")
    public Result<SysMenuDetailsRespVO> getMenuDetailsById(@PathVariable(name = "id") Long id){
        SysMenuDetailsRespVO sysMenuDetailsRespVO = sysMenuService.getMenuDetailsById(id);
        return Result.success(sysMenuDetailsRespVO);
    }

    @PreAuthorize("hasAuthority('sys:menu:list')")
    @GetMapping("list")
    public Result<List<SysMenuTreeRespVO>> getMenuTreeList(){
        List<SysMenuTreeRespVO> sysMenuTreeRespVOList = sysMenuService.getMenuTreeList();
        return Result.success(sysMenuTreeRespVOList);
    }

    @PreAuthorize("hasAuthority('sys:menu:save')")
    @PostMapping("/save")
    public Result<Void> saveMenu(@Validated @RequestBody SysMenuSaveReqVO sysMenuSaveReqVO){
        sysMenuService.saveMenu(sysMenuSaveReqVO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:menu:update')")
    @PostMapping("/update")
    public Result<Void> updateMenu(@Validated @RequestBody SysMenuUpdateReqVO sysMenuUpdateReqVO){
        sysMenuService.updateMenu(sysMenuUpdateReqVO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @PostMapping("/delete/{id}")
    public Result<Void> deleteById(@PathVariable(name = "id") Long id){
        sysMenuService.deleteById(id);
        return Result.success();
    }

}
